package com.dev.security.login.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.PatternMatchUtils;

import com.dev.security.login.beans.LoginSource;
import com.dev.security.login.beans.LoginUser;
import com.dev.security.login.service.UserService;
import com.dev.security.manager.beans.Account;
import com.dev.security.manager.beans.App;
import com.dev.security.manager.dao.AccountRepository;
import com.dev.security.manager.dao.AccountRoleUnitRepository;
import com.dev.security.manager.dao.AppRepository;
import com.dev.security.manager.dao.RoleAppUnitRepository;
import com.dev.security.manager.dao.RolePermUnitRepository;
import com.dev.security.manager.dao.SourceRepository;

/**
 * @author 潜行的青衣
 * @see
 */
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private AccountRepository accountRepository;
	
	@Autowired
	private SourceRepository sourceRepository;
	
	@Autowired
	private AppRepository appRepository;
	
	@Autowired
	private RolePermUnitRepository rolePermUnitRepository;
	
	@Autowired
	private RoleAppUnitRepository roleAppUnitRepository;
	
	@Autowired
	private AccountRoleUnitRepository accountRoleUnitRepository;
	
	/* (non-Javadoc)
	 * @see com.dev.security.login.service.UserService#findLoginUser(java.lang.String)
	 */
	@Override
	public LoginUser findLoginUser(String user_id) {
		Account account = this.accountRepository.findOne(user_id);
		if(account != null) {
			account.setRole_ids(accountRoleUnitRepository.findRoleIds(user_id));
		}
		return account;
	}

	/* (non-Javadoc)
	 * @see com.dev.security.login.service.UserService#findLoginUser(java.lang.String, java.lang.String)
	 */
	@Override
	public LoginUser findLoginUser(String user_account, String status) {
		Account account = this.accountRepository.findOne(user_account, status);
		if(account != null) {
			account.setRole_ids(accountRoleUnitRepository.findRoleIds(account.getId()));
		}
		return account;
	}

	/* (non-Javadoc)
	 * @see com.dev.security.login.service.UserService#isForbid(com.dev.security.login.beans.LoginUser, java.lang.String)
	 */
	@Override
	public boolean isForbid(LoginUser user, LoginSource source) {
		if(source != null) {
			List<String> role_ids = user.getRoleIds();
			if(source instanceof App) {
				return this.roleAppUnitRepository.countApps(role_ids, source.getType()) == 0;
			}
			return this.rolePermUnitRepository
					.countPermissions(role_ids, source.getType()) == 0;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.dev.security.login.service.UserService#findLoginSource(java.lang.String)
	 */
	@Override
	public LoginSource findLoginSource(String uri) {
		if(PatternMatchUtils.simpleMatch("/resources/apps/*.js", uri)) {
			LoginSource source = this.appRepository.findByUri(uri);
			if(source != null) {
				return source;
			}
		}
		return this.sourceRepository.findOne(uri);
	}

}
